<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2025/2/7
 * Time: 10:52
 */

namespace app\controller\index;


class Deepseek extends Base
{
    private $apikey = 'sk-db2806f77fb648769ff73b434b85d0e3';
    public function info(){
        return $this->view('/index/deepseek/info');
    }
    public function message(){
        // 替换为您的API密钥
        $api_key = $this->apikey;
        // API端点URL
        $url = 'https://api.deepseek.com/chat/completions';
        // 设置请求头
        $headers = array(
            "Authorization: Bearer {$api_key}",
            "Content-Type: application/json"
        );
        // 构造请求体（如果有的话）
        $payload = array(
            'model' => 'deepseek-chat',
            'data' => input('message','')
        );
        // 初始化cURL会话
        $ch = curl_init();
        // 设置cURL选项
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // 发送请求并获取响应
        $response = curl_exec($ch);
        // 检查是否有错误发生
        if (curl_errno($ch)) {
            $err = 'Error:' . curl_error($ch);
        } else {
            // 处理响应
            $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            if ($http_code == 200) {
                $data = json_decode($response, true);
            } else {
                $msg = "Error: HTTP status code {$http_code}\n";
                $data =  $response;
            }
        }
        // 关闭cURL会话
        curl_close($ch);
        if (!empty($err))
            return $this->fail($err);
        if ($http_code == 200) {
            return $this->msg($data);
        }else{
            return $this->fail($msg,$data);
        }
    }
}